<template>
  <div class="app-container">
    <div class="search-container">
      <el-form :model="searchParam" :inline="true">
        <el-form-item label="消息类别名称">
          <el-input
            v-model="searchParam.templeName"
            placeholder="请输入消息类别名称"
            clearable
            @keyup.enter="search"
          />
        </el-form-item>
        <el-form-item label="消息状态">
          <el-select
            v-model="searchParam.messageStatus"
            placeholder="请选择消息状态"
            clearable
          >
            <el-option :value="0" label="发送中" />
            <el-option :value="1" label="发送成功" />
            <el-option :value="2" label="发送失败" />
            <el-option :value="3" label="发送异常" />
          </el-select>
        </el-form-item>
        <el-form-item label="消息内容">
          <el-input
            v-model="searchParam.content"
            placeholder="请输入消息内容"
            clearable
            @keyup.enter="search"
          />
        </el-form-item>
        <el-form-item label="消息模式">
          <el-select
            v-model="searchParam.messageMode"
            placeholder="请选择消息模式"
            clearable
          >
            <el-option
              v-for="item in useDictionary('MessageMode')"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="消息级别">
          <el-select
            v-model="searchParam.messageLevel"
            placeholder="请选择消息级别"
            clearable
          >
            <el-option
              v-for="item in useDictionary('MessageLevel')"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="发送时间">
          <el-date-picker
            v-model="searchParam.sendTimeSearch"
            type="datetimerange"
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
            value-format="YYYY-MM-DD HH:mm:ss"
            @change="getsendTime"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="search">
            <i-ep-search />
            搜索
          </el-button>
          <el-button @click="reset">
            <i-ep-refresh />
            重置
          </el-button>
          <el-button
            class="ml-3"
            @click="
              useDownload(
                '/system/sysmessagesendrecord/export',
                '导出表格名称',
                searchParam
              )
            "
            style="display: none"
          >
            <template #icon>
              <i-ep-download />
            </template>
            导出
          </el-button>
        </el-form-item>
      </el-form>
    </div>

    <el-card shadow="never" class="table-container">
      <el-table
        ref="dataTableRef"
        v-loading="loading"
        :data="tableData"
        highlight-current-row
        border
        @selection-change="handleSelectionChange"
      >
        <el-table-column label="消息类别名称" prop="typeName" min-width="120">
        </el-table-column>
        <el-table-column label="消息模式" prop="messageMode" min-width="100">
          <template #default="scope">
            {{ useDictionary("MessageMode", scope.row.messageMode) }}
          </template>
        </el-table-column>
        <el-table-column label="消息级别" prop="messageLevel" min-width="100">
          <template #default="scope">
            {{ useDictionary("MessageLevel", scope.row.messageLevel) }}
          </template>
        </el-table-column>

        <el-table-column label="接收人数量" prop="reciveCount" min-width="100">
          <template #default="scope">
            {{
              Number(scope.row.reciveCount) + Number(scope.row.reciveErrCount)
            }}
          </template>
        </el-table-column>
        <el-table-column label="消息标题" prop="title" min-width="100">
        </el-table-column>
        <el-table-column
          label="消息内容"
          prop="content"
          min-width="100"
          align="center"
          show-overflow-tooltip
        >
        </el-table-column>
        <el-table-column label="发送时间" prop="sendTime" min-width="160" align="center">
        </el-table-column>
        <el-table-column label="消息状态" min-width="100" align="center">
          <template #default="scope">
            <el-tag v-if="scope.row.messageStatus == 0"> 发送中 </el-tag>
            <el-tag v-if="scope.row.messageStatus == 1" type="success">
              发送成功
            </el-tag>
            <el-tag v-if="scope.row.messageStatus == 3" type="warning">
              发送异常
            </el-tag>
            <el-tag v-if="scope.row.messageStatus == 2" type="danger"
              >发送失败</el-tag
            >
          </template>
        </el-table-column>
        <el-table-column fixed="right" label="操作" width="80" align="center">
          <template #default="scope">
            <el-button
              type="primary"
              size="small"
              link
              @click="openDialogdetail(scope.row)"
            >
              <i-ep-edit />
              详情
            </el-button>
          </template>
        </el-table-column>
      </el-table>

      <el-pagination
        :background="true"
        :current-page="pageable.pageNum"
        :page-size="pageable.pageSize"
        :page-sizes="[10, 25, 50, 100]"
        :total="pageable.total"
        layout="total, sizes, prev, pager, next, jumper"
        class="mt-[20px]"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </el-card>

    <sysmessagesendrecord-edit
      :visible="addDialog.visible"
      :title="addDialog.title"
      :data="addDialog.data"
      @reset-table="reset"
      :is-readonly="addDialog.readvisible"
      @change-visible="changeVisible"
    />
  </div>
</template>

<script setup lang="ts">
import { useTable, useDownload, useDictionary } from "@/utils/useTable";
import request from "@/utils/request";

defineOptions({
  name: "SYSMESSAGESENDRECORD",
  inheritAttrs: false,
});

// 请求参数
const requestApi = "/system/sysmessagesendrecord/queryByPage";

// 是否需要分页
const pagination = true;

const initParam = {};

// 初始化搜索参数数据
const searchParamInit = {
  templeId: "",
  templeName: "",
  recevieUserId: "",
  recevieUserInfo: "",
  messageStatus: "",
  messageLevel: "",
  messageMode: "",
  email: "",
  phone: "",
  content: "",
  sendTimeSearch: [],
  sendTimeStart: "",
  sendTimeEnd: "",
};

const deleteParam = {
  api: "/system/sysmessagesendrecord/delete",
  data: {},
};

// 数据请求错误后处理
function requestError() {}

const dataCallBack = ref(undefined); // 无需二次数据处理&不传赞数也行

// 表格操作 Hooks
const {
  tableData,
  pageable,
  searchParam,
  ids,
  getTableList,
  search,
  reset,
  handleSizeChange,
  handleCurrentChange,
  handleSelectionChange,
  handleTableDelete,
  loading,
}: any = useTable(
  requestApi,
  initParam,
  searchParamInit,
  pagination,
  dataCallBack.value,
  requestError,
  deleteParam
);

onMounted(() => {
  getTableList();
});

const addDialog = reactive<DialogOption>({
  visible: false,
  readvisible: false,
  title: "",
  data: {},
});

// 查询详情
async function getInfo(data: any) {
  console.log(data);
  addDialog.data = data;
}

/** 打开email消息发送记录表单弹窗 */
function openDialog(roleObj?: Object) {
  if (roleObj) {
    addDialog.title = "修改email消息发送记录";
    getInfo(roleObj);
  } else {
    addDialog.title = "新增email消息发送记录";
  }
  addDialog.readvisible = false;
  addDialog.visible = true;
}

/** 打开email消息发送记录表单弹窗 详情 */
function openDialogdetail(roleObj?: any) {
  addDialog.title = "消息发送记录详情";
  getInfo(roleObj);
  addDialog.readvisible = true;
  addDialog.visible = true;
}

function changeVisible(value: boolean) {
  addDialog.visible = value;
  if (!value) {
    addDialog.data = {};
  }
}

function getsendTime(data: any) {
  if (data) {
    searchParam.value.sendTimeStart = data[0];
    searchParam.value.sendTimeEnd = data[1];
  } else {
    searchParam.value.sendTimeStart = "";
    searchParam.value.sendTimeEnd = "";
  }
}
</script>
<style lang="scss" scoped>
:deep(.el-form .el-form-item__label) {
  width: 91px;
}
</style>
